/*
 * SimpleWorld.cpp
 *
 *  Created on: Jun 29, 2010
 *      Author: Ry
 */

#include <iostream>
#include "SimpleWorld.h"

using namespace std;

SimpleWorld::SimpleWorld() {
	Object *object = newObject();
	object->setName("CameraObject");
	defaultCamera = new Camera(object);
	camera = defaultCamera;
}

Object* SimpleWorld::newObject() {
	Object *object = new Object();
	objects.push_back(object);
	return object;
}

Object* SimpleWorld::newObject(Prototype *prototype) {
	Object *object = new Object(prototype);
	objects.push_back(object);
	return object;
}

void SimpleWorld::addObject(Object *object) {
	objects.push_back(object);
}

void SimpleWorld::step(float dts) {
	for (unsigned int i = 0; i < objects.size(); i++) {
		MotionController *mc = objects[i]->getMotionController();
		if (mc != NULL) {
			mc->step(dts);
		}
	}
}

void SimpleWorld::draw() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	if (camera == NULL) {
		// Don't render anything at all
	} else {
		camera->view();
		for (unsigned int i = 0; i < objects.size(); i++) {
			glPushMatrix();
			objects[i]->draw();
			glPopMatrix();
		}
	}
}

SimpleWorld::~SimpleWorld() {
	delete defaultCamera;
	//cout << "Deleting " << objects.size() << " objects from the world." << endl;
	for (unsigned int i = 0; i < objects.size(); i++) {
		delete objects[i];
	}
}
